package com.xique.door.controller;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.DCard;
import com.xique.door.bean.request.card.CardAddRequest;
import com.xique.door.bean.request.card.CardCommandSearchRequest;
import com.xique.door.biz.service.ICardBizService;
import com.xique.door.service.IDCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 卡片Controller
 *
 * @author ww
 * @date 2021-09-14
 */
@RestController
@RequestMapping("/card")
public class DCardController extends BaseController {

    @Autowired
    private IDCardService dCardService;

    @Autowired
    private ICardBizService cardBizService;


    /**
     * 查询某个用户的卡片列表
     */
    @PreAuthorize(hasPermi = "door:card:list")
    @GetMapping("/userCard/list")
    public TableDataInfo userCardList(DCard dCard) {
        startPage();
        dCard.setParkId(SecurityUtils.getParkId());
        return cardBizService.selectCardList(dCard);
    }



    /**
     * 查询某个用户的卡片列表
     */
    @PreAuthorize(hasPermi = "door:card:list")
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("id") String id) {
        return cardBizService.detail(id);
    }

    /**
     * 导出卡片列表
     */
    @PreAuthorize(hasPermi = "door:card:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DCard dCard) throws IOException {
        List<DCard> list = dCardService.selectDCardList(dCard);
        ExcelUtil<DCard> util = new ExcelUtil<DCard>(DCard.class);
        util.exportExcel(response, list, "card");
    }


    /**
     * 新增保存卡片
     * 1、判断该小区该卡号是否已经添加;
     * 2、数据库中添加记录：
     * 1）判断该用户
     * 3、信息下发到设备；
     */
    @PreAuthorize(hasPermi = "door:card:add")
    @Log(title = "卡片", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody CardAddRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return cardBizService.addCard(request);

    }

    /**
     * 同步单卡片至设备
     *
     * @return
     */
    @Log(title = "同步单卡片至设备", businessType = BusinessType.COMMAND)
    @GetMapping("/downSingleCard")
    public AjaxResult downSingleCard(Long cardId) {
        DCard dCard = dCardService.selectDCardById(cardId);
        //如果状态为禁用，点击下发，填充0000000000，原本卡号不可用
        if ("0".equals(dCard.getStatus())) {
            dCard.setCardSn("0000000000");
        }
        return cardBizService.downCard(dCard,null);
    }

    /**
     * 修改保存卡片
     */
    @PreAuthorize(hasPermi = "door:card:edit")
    @Log(title = "卡片", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editSave(@RequestBody DCard card) {
//        int count = dCardService.updateDCard(card);
        //如果状态为禁用，点击下发，填充0000000000，原本卡号不可用
        int i = dCardService.updateDCard(card);
        if (i<0){
            return AjaxResult.error("修改失败");
        }
        DCard downCard = dCardService.selectDCardById(card.getId());
        if ("0".equals(card.getStatus())) {
            downCard.setCardSn("0000000000");
        }
       return cardBizService.downCard(downCard,null);
    }

    /**
     * 卡片（挂失），不是删除
     * 1、数据库中修改该卡片记录为禁用；
     * 2、下发设备，卡号默认填充0000000000
     */
    @PreAuthorize(hasPermi = "door:card:edit")
    @Log(title = "卡片", businessType = BusinessType.UPDATE)
    @DeleteMapping("/loss")
    @Transactional
    public AjaxResult reportLoss(Long cardId) {
        return  cardBizService.loss(cardId);
    }

    /**
     * 卡片解挂
     * 1、数据库中修改该卡片记录为禁用；
     * 2、下发设备，卡号默认填充0000000000
     */
    @PreAuthorize(hasPermi = "door:card:edit")
    @Log(title = "卡片", businessType = BusinessType.UPDATE)
    @GetMapping("/unLoss")
    @Transactional
    public AjaxResult unLoss(Long cardId) {
        return cardBizService.unLoss(cardId);
    }

    /**
     * 注销卡片（删除）
     * 1、数据库中修改该卡片记录为禁用；
     * 2、下发设备，卡号默认填充0000000000
     */
    @PreAuthorize(hasPermi = "door:card:remove")
    @Log(title = "卡片", businessType = BusinessType.DELETE)
    @DeleteMapping("/logout")
    @Transactional
    public AjaxResult cancel(Long cardId) {
        return cardBizService.deleteCard(cardId);
    }


    /**
     * 查询某个用户的卡片列表
     */
    @PreAuthorize(hasPermi = "door:card:list")
    @GetMapping("/condition/list")
    public TableDataInfo conditionList(DCard dCard) {
        startPage();
        dCard.setParkId(SecurityUtils.getParkId());
        List<DCard> list = dCardService.selectConditionCardList(dCard);
        return getDataTable(list);
    }


    /**
     * 查询卡片列表
     */
    @PreAuthorize(hasPermi = "door:card:list")
    @GetMapping("/manage/list")
    public TableDataInfo list(DCard dCard) {
        startPage();
        dCard.setParkId(SecurityUtils.getParkId());
        List<DCard> list = dCardService.selectDCardList(dCard);
        return getDataTable(list);
    }


    @Log(title = "发行卡", businessType = BusinessType.COMMAND)
    @PostMapping("/manage/add")
    public AjaxResult addCard(@RequestBody CardAddRequest request) {

        return cardBizService.addCard(request);
    }

    @Log(title = "下发", businessType = BusinessType.COMMAND)
    @PostMapping("/manage/downCard")
    public AjaxResult downCard(@RequestBody CardAddRequest request) {

        DCard card = dCardService.selectDCardById(request.getCardId());
        return cardBizService.downCard(card,null);
    }


    /**
     * 修改保存卡片
     */
    @PreAuthorize(hasPermi = "door:card:edit")
    @Log(title = "卡片", businessType = BusinessType.UPDATE)
    @PutMapping("/manage/edit")
    public AjaxResult editCard(@RequestBody DCard dCard) {
        int count = dCardService.updateDCard(dCard);
        //如果状态为禁用，点击下发，填充0000000000，原本卡号不可用
        if ("0".equals(dCard.getStatus())) {
            dCard.setCardSn("0000000000");
        }
        if (count > 0) {
            //重新下发卡片
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 卡片（挂失），不是删除
     * 1、数据库中修改该卡片记录为禁用；
     * 2、下发设备，卡号默认填充0000000000
     */
    @PreAuthorize(hasPermi = "door:card:edit")
    @Log(title = "卡片", businessType = BusinessType.UPDATE)
    @DeleteMapping("/manage/loss")
    @Transactional
    public AjaxResult manageLoss(Long cardId) {

        return cardBizService.loss(cardId);
    }

    /**
     * 卡片解挂
     * 1、数据库中修改该卡片记录为禁用；
     * 2、下发设备，卡号默认填充0000000000
     */
    @PreAuthorize(hasPermi = "door:card:edit")
    @Log(title = "卡片", businessType = BusinessType.UPDATE)
    @GetMapping("/manage/unLoss")
    @Transactional
    public AjaxResult manageUnLoss(Long cardId) {
        return cardBizService.unLoss(cardId);
    }

    /**
     * 注销卡片（删除）
     * 1、数据库中修改该卡片记录为禁用；
     * 2、下发设备，卡号默认填充0000000000
     */
    @PreAuthorize(hasPermi = "door:card:remove")
    @Log(title = "卡片", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteCard")
    public AjaxResult deleteCard(Long cardId) {
        return cardBizService.deleteCard(cardId);
    }



    /**
     * 查询某个用户的卡片列表
     */
    @PreAuthorize(hasPermi = "door:card:list")
    @GetMapping("/command/list")
    public TableDataInfo cardCommandList(CardCommandSearchRequest request) {
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        return cardBizService.selectCardCommandList(request);
    }


}
